package com.example.screenplayer;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private SocketClientManager mSocketClientManager;
private String ip = "";  // ip 地址
private TextureView textureView = null;
private Button button = null;
private boolean isSocketManagerInitialized = false;
private String mScreenIp;
private String mScreenDevices;
private boolean mScreenInitialized = false;
private List<String> materialMutableList = new ArrayList<>();
private MediaProjectionManager mediaProjectionManager;

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate (Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.btn);
        textureView = findViewById(R.id.tv_screen);


        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {

                @Override
                public void onSurfaceTextureAvailable (@NonNull SurfaceTexture surface, int width, int height) {
                        Log.d("xxxx", "111111111111");

                        // 使用 SurfaceTexture 创建 Surface


                }

                @Override
                public void onSurfaceTextureSizeChanged (@NonNull SurfaceTexture surface, int width, int height) {
                        Log.d("xxxx", "222222222222");
                }

                @Override
                public boolean onSurfaceTextureDestroyed (@NonNull SurfaceTexture surface) {
                        return true;
                }

                @Override
                public void onSurfaceTextureUpdated (@NonNull SurfaceTexture surface) {


                }
        });
        button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick (View v) {
                        SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
                        if (!mScreenIp.isEmpty()) {
                                if (surfaceTexture != null && !isSocketManagerInitialized) {
                                        Log.d("xxxx", "SurfaceTexture already available. Using it directly.");
                                        Surface mySurface = new Surface(surfaceTexture);
                                        initSocketManager(mySurface, mScreenIp);
                                        isSocketManagerInitialized = true; // 避免重复初始化
                                }
                        } else {
                                Toast.makeText(MainActivity.this, "未找到服务端", Toast.LENGTH_SHORT).show();
                        }

                }
        });

        EventBus.getDefault().register(this);

        // 启动服务来监听 UDP 广播
        Intent intent = new Intent(this, UDPReceiverService.class);
        startService(intent);


}


private void initSocketManager (Surface surface, String ip) {
        mSocketClientManager = new SocketClientManager();
        mSocketClientManager.start(surface, ip);
}

@Override
protected void onDestroy () {
        if (mSocketClientManager != null) {
                mSocketClientManager.stop();
        }
        // 停止服务
        Intent intent = new Intent(this, UDPReceiverService.class);
        stopService(intent);
        EventBus.getDefault().unregister(this);

        super.onDestroy();
}


@Subscribe(threadMode = ThreadMode.MAIN)
public void onGetMessage (ScreeIpAddress message) {
        NetworkUtils NetWorkUtil = null;
        mScreenIp = message.serverIp;  // 获取 message 的 ip

        // 判断接收到的消息是否与上次接收到的相同
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!message.receivedMessage.equals(mScreenIp) && !NetWorkUtil.getLocalIpAddress(this).equals(mScreenIp) && !mScreenInitialized) {
                        mScreenDevices = message.receivedMessage;  // 更新上次接收到的消息
                        materialMutableList.add(mScreenDevices);
                } else {
                        Log.d("xxxx", "接收到重复的广播信息，不发送");
                }
        }
}

}
